package com.gxwzxy.medic.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.gxwzxy.medic.dao.AreaDao;
import com.gxwzxy.medic.dao.Impl.AreaDaoImpl;
import com.gxwzxy.medic.entity.Area;
import com.gxwzxy.medic.entity.Menu;
import com.gxwzxy.medic.entity.otherentity.OtherArea;
import com.gxwzxy.medic.service.AreaService;
import com.gxwzxy.medic.service.Impl.AreaServiceImpl;
import com.gxwzxy.medic.until.PageBean;
import com.my.web.servlet.RequestBeanUtils;

/**
 * Servlet implementation class AreaSevrlet
 */
public class AreaServlet extends BaseServlet{
	private static final long serialVersionUID = 1L;
       
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
    public void findOtherArea(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
    	String pid = request.getParameter("parentid");
		AreaDao ad = new AreaDaoImpl();
		List<OtherArea> la =  ad.getAreaInfoDao(pid);
		response.getWriter().write(new Gson().toJson(la));
    }
    
    /**
     * 查询行政区域分页展示
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
	public void findArea (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
		//获取前端传来对的index
    	String sindex = request.getParameter("index");
    	int index = 1;
		try {
    		index = Integer.parseInt(sindex);
    	}catch(NumberFormatException n) {
    		n.printStackTrace();
    	}
    	
    	PageBean<Area> pu = new PageBean<Area>();
    	pu.setIndex(index);//设置展示的当前页数
    	AreaService as = new AreaServiceImpl();
    	as.pageArea(pu);
		request.setAttribute("pu", pu);
		request.getRequestDispatcher("page/area/area.jsp").forward(request, response);
	}
	
	/**
	 * 查询所有行政区域
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void findAllArea(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
		//处理响应请求，直接查询所有行政区域
		AreaService as = new AreaServiceImpl();
		List <Area> la = as.findAllArea();
		request.setAttribute("la", la);
		request.getRequestDispatcher("page/area/addArea.jsp").forward(request, response);
	}
	/**
	 * 新建行政区域
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void addArea(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
		//获取请求数据
		String area = request.getParameter("area");
		String newareaName = request.getParameter("areaName");//新行政区域名称
		//将option的value值分割
		String [] areaA = area.split(",");
		String oldareaCode= areaA[0];//所属的行政区域编号
		String oldgrade = areaA[1];//所属的行政区域等级
		//处理请求
		AreaService as = new AreaServiceImpl();
		boolean bl = as.addArea(oldareaCode,oldgrade,newareaName);
		if(bl) {
			request.setAttribute("url", "AreaServlet?method=findArea&&index=1");
			request.getRequestDispatcher("tips.jsp").forward(request, response);
		}
	}
	
	/**
	 * 根据areaCode查询行政区域
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void findAreaByareaId(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
		//获取请求数据
		String areaCode = request.getParameter("areaCode");
		//处理请求
		AreaService as = new AreaServiceImpl();
		List<Area> la = as.findAreaByareaCode(areaCode);
		request.setAttribute("la", la);
		request.getRequestDispatcher("page/area/updateArea.jsp").forward(request, response);
	}
	
	/**
	 * 根据areaCode修改行政区域
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void updateArea(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
		//声明变量
		Area a = new Area();
		//获取请求数据
		String areaCode = request.getParameter("areaCode");
		String areaName = request.getParameter("areaName");
		String grade = request.getParameter("grade");
		//处理请求
		a.setAreaCode(areaCode);
		a.setAreaName(areaName);
		a.setGrade(grade);
		AreaService as = new AreaServiceImpl(); 
		if(as.updateArea(a)) {
			request.setAttribute("url", "AreaServlet?method=findArea&&index=1");
			request.getRequestDispatcher("tips.jsp").forward(request, response);
		}
	}
	
	/**
	 * 根据areaCode删除行政区域
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void deleteArea(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
		//获取请求数据
		String areaCoed = request.getParameter("areaCode");
		//处理请求
		AreaService as = new AreaServiceImpl();
		if(as.deleteArea(areaCoed)) {
			response.getWriter().write("true");
		}
	}
	
	/**
	 * 行政区域联动
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void ldArea(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
		//获取请求数据
		Area a = RequestBeanUtils.requestToBean(request, Area.class);
		//处理请求
		AreaService as = new AreaServiceImpl();
		List<Area> la = as.ldArea(a);
		response.getWriter().write(new Gson().toJson(la));
	}
}
